var HxBasicGoodsInfoDlg = {
    data: {
		id:"",
        goodsCustomerId: "",
        goodsCustomerName: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsXcode69: "",
        goodsHelpcode: "",
        goodsSpecs: "",
        goodsBigtype: "",
        goodsBrand: "",
        jibenUnit: "",
        kcUnit: "",
        kcRate: "",
        cgUnit: "",
        cgRate: "",
        pfUnit: "",
        pfRate: "",
        psUnit: "",
        psRate: "",
        anquanNum: "",
        goodsBottom: "",
        goodsLamination: "",
        goodsTraylayer: "",
        goodsSupportvolume: "",
        length: "",
        height: "",
        width: "",
        goodsWeight: "",
        goodsVolume: "",
        goodsChargingCg: "",
        goodsChargingPs: "",
        goodsChargingPf: "",
        goodsWarranty: ""
    }
};
var BasicPutgoodsInfoDlg = {
    data: {
		id:"",
		dictId:"",
        putgoodsCustomerId: "",
        putgoodsOperateName: "",
        putgoodsName: "",
		unitNumber:"",
		unitType:""
    }
};
var submit = 2
layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startTime'
        	,value:new Date()
    });
    
    laydate.render({
    	elem:'#endTime'
        	,value:new Date()
    });

    /**
     * 入库单明细表管理
     */
    var HxStoragePutDetailed = {
        tableId: "hxStoragePutDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    HxStoragePutDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'printNum', sort: true, title: '打印次数'},
            {field: 'getWarehouse', sort: true, title: '收货仓库'},
            {field: 'setUnit', sort: true, title: '发货客户', width: 140},
            {field: 'storageNumber', sort: true, title: '入库单编号', width: 140},
            {field: 'trayNumber', sort: true, title: '托盘编号'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'unit', sort: true, title: '单位', totalRowText: '合计：'},

            {field: 'num', sort: true, title: '数量',totalRowText: sl.num},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'jibenNum', sort: true, title: '基本数量'},
            {field: 'jibenRealNum', sort: true, title: '实际基本数量'},

            {field: 'realNum', sort: true, title: '实际数量', totalRowText: sl.realNum},
            {field: 'goodsWeight', sort: true, title: '重量'},
            {field: 'putWeight', sort: true, title: '应入重量', totalRowText: sl.putWeight},
            {field: 'goodsVolume', sort: true, title: '体积'},
            {field: 'putVolume', sort: true, title: '应入体积', totalRowText: sl.putVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'putMoney', sort: true, title: '金额', totalRowText: sl.putMoney},
            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'expirationDate', sort: true, title: '失效日期'},
            {field: 'putState', sort: true, title: '入库状态', templet: '#putStateTpl'},
            {field: 'qualityUserName', sort: true, title: '质检人'},
            {field: 'qualityTime', sort: true, title: '质检时间'},
            {field: 'putUserName', sort: true, title: '上架人'},
            {field: 'putTime', sort: true, title: '上架时间'},
            {field: 'positionNumber', sort: true, title: '上架库位'},
            {field: 'revokeName', sort: true, title: '撤销人'},
            {field: 'nodes', sort: true, title: '备注'},
        ]];
    };
    var sl = {
        putVolume: "",
        realNum: "",
        num: "",
        putWeight: "",
        putMoney: "",
    };
    // 发送 AJAX 请求获取合计数据
    $.ajax({
        url:Feng.ctxPath + '/hxStoragePutDetailed/count',
        dataType: "json",
        async: false,
        type: "post",
        success: function (data) {
            console.log(data);
            sl = data.data;
            console.log(sl);
        },
        error: function (xhr, status, error) {
            console.error("请求失败：", status, error);

        }
    });


    /**
     * 点击查询按钮
     */
    HxStoragePutDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['storageState'] = $("#storageState").val();
        queryData['timeType'] = $("#timeType").val();
        queryData['trayNumber'] = $("#trayNumber").val();
        queryData['printNum'] = $("#printNum").val();

        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['putState'] = $("#putState").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['getWarehouseId'] = $("#getWarehouseId").val();
        queryData['setUnits'] = $("#setUnit").val();
        queryData['orderType'] = $("#orderType").val();
        queryData['goodsNames'] = $("#goodsName").val();

        queryData['qualityUserName'] = $("#qualityUserName").val();
        queryData['putUserName'] = $("#putUserName").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        // table.reload(HxStoragePutDetailed.tableId, {
        //     where: queryData, page: {curr: 1},
        //      done:function () {
        //     }
        // });
        $.ajax({
            url:Feng.ctxPath + '/hxStoragePutDetailed/count',
            data: queryData, // 将 queryData 作为请求体发送
            dataType:"json",   //返回格式为json
            async: false,
            type:"post",   //请求方式
            success:function(data){
                sl = data.data
                console.log(data);
            }
        });
        // 渲染表格
        var tableResult = table.render({
            elem: '#' + HxStoragePutDetailed.tableId,
            where: queryData,
            url: Feng.ctxPath + '/hxStoragePutDetailed/qualitylist',

            height: "full-60",
            toolbar: '#toolbarfrom',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: HxStoragePutDetailed.initColumn()
            ,parseData: function(res){ // 数据转换
                return {
                    "code": 0,
                    "msg": res.msg, // 假设原数据中的msg字段存储的是提示信息
                    "count": res.count,
                    "data": $.map(res.data, function(item){ // 假设原数据中的data字段存储的是数据集
                        return {
                            // 假设你想转换的字段是numberField
                            realNum: Number(item.realNum), // 将字符串转换为数字
                            num: Number(item.num), // 将字符串转换为数字
                            putWeight: item.putWeight,
                            putVolume: item.putVolume,
                            putMoney: item.putMoney,
                            goodsWeight: item.goodsWeight,
                            goodsVolume: item.goodsVolume,
                            goodsPrice: item.goodsPrice,
                            putState: item.putState,
                            unit: item.unit,
                            jibenUnit: item.jibenUnit,
                            jibenNum: item.jibenNum,
                            jibenRealNum: item.jibenRealNum,
                            id: item.id,
                            printNum: item.printNum,
                            getWarehouse: item.getWarehouse,
                            setUnit: item.setUnit,
                            storageNumber: item.storageNumber,
                            trayNumber: item.trayNumber,
                            goodsName: item.goodsName,
                            goodsNumber: item.goodsNumber,
                            goodsPcode69: item.goodsPcode69,
                            goodsSpecs: item.goodsSpecs,
                            goodsBigtype: item.goodsBigtype,
                            goodsBrand: item.goodsBrand,
                            customerName: item.customerName,
                            qualityUserName: item.qualityUserName,
                            qualityTime: item.qualityTime,
                            putUserName: item.putUserName,
                            putTime: item.putTime,
                            positionNumber: item.positionNumber,
                            nodes: item.nodes,
                            detailedDate: item.detailedDate,
                            revokeName: item.revokeName,
                            expirationDate: item.expirationDate,
                        };
                    })
                };
            },
            page: true,
            limit: 40,
            limits: [40, 80, 120, 200, 500,1000,2000,5000],
        });
    };
    
    //点击选择商品名称
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '选择商品',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods',
            end: function () {
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);//商品名称
            }
        });
    });
    
    $('#choiceSetUnit').click(function () {
        layer.open({
            type: 2,
            title: '选择单位',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPutgoods/tochoiceBasicPutgoods_hx',
            end: function () {
                $("#setUnit").val(BasicPutgoodsInfoDlg.data.putgoodsName);
            }
        });
    });

    /**
     * 弹出添加对话框
     */
    HxStoragePutDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hxStoragePutDetailed/add';
    };

    /**
     * 导出excel按钮
     */
    HxStoragePutDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(HxStoragePutDetailed.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['condition'] = $("#condition").val();
            queryData['storageState'] = $("#storageState").val();
            queryData['timeType'] = $("#timeType").val();
            queryData['trayNumber'] = $("#trayNumber").val();
            queryData['printNum'] = $("#printNum").val();

            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['putState'] = $("#putState").val();
            queryData['customerId'] = $("#customerId").val();
            queryData['getWarehouseId'] = $("#getWarehouseId").val();
            queryData['setUnits'] = $("#setUnit").val();
            queryData['orderType'] = $("#orderType").val();
            queryData['goodsNames'] = $("#goodsName").val();

            queryData['qualityUserName'] = $("#qualityUserName").val();
            queryData['putUserName'] = $("#putUserName").val();queryData['condition'] = $("#condition").val();
            queryData['storageState'] = $("#storageState").val();
            queryData['timeType'] = $("#timeType").val();
            queryData['trayNumber'] = $("#trayNumber").val();
            queryData['printNum'] = $("#printNum").val();

            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['putState'] = $("#putState").val();
            queryData['customerId'] = $("#customerId").val();
            queryData['getWarehouseId'] = $("#getWarehouseId").val();
            queryData['setUnits'] = $("#setUnit").val();
            queryData['orderType'] = $("#orderType").val();
            queryData['goodsNames'] = $("#goodsName").val();

            queryData['qualityUserName'] = $("#qualityUserName").val();
            queryData['putUserName'] = $("#putUserName").val();

            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/hxStoragePutDetailed/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxStoragePutDetailed.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxStoragePutDetailed/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxStoragePutDetailed.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxStoragePutDetailed/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxStoragePutDetailed.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxStoragePutDetailed.tableId,
        url: Feng.ctxPath + '/hxStoragePutDetailed/qualitylist',
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: HxStoragePutDetailed.initColumn()
        ,parseData: function(res){ // 数据转换
            return {
                "code": 0,
                "msg": res.msg, // 假设原数据中的msg字段存储的是提示信息
                "count": res.count,
                "data": $.map(res.data, function(item){ // 假设原数据中的data字段存储的是数据集
                    return {
                        // 假设你想转换的字段是numberField
                        realNum: Number(item.realNum), // 将字符串转换为数字
                        num: Number(item.num), // 将字符串转换为数字
                        putWeight: item.putWeight,
                        putVolume: item.putVolume,
                        putMoney: item.putMoney,
                        goodsWeight: item.goodsWeight,
                        goodsVolume: item.goodsVolume,
                        goodsPrice: item.goodsPrice,
                        putState: item.putState,
                        unit: item.unit,
                        jibenUnit: item.jibenUnit,
                        jibenNum: item.jibenNum,
                        jibenRealNum: item.jibenRealNum,
                        id: item.id,
                        printNum: item.printNum,
                        getWarehouse: item.getWarehouse,
                        setUnit: item.setUnit,
                        storageNumber: item.storageNumber,
                        trayNumber: item.trayNumber,
                        goodsName: item.goodsName,
                        goodsNumber: item.goodsNumber,
                        goodsPcode69: item.goodsPcode69,
                        goodsSpecs: item.goodsSpecs,
                        goodsBigtype: item.goodsBigtype,
                        goodsBrand: item.goodsBrand,
                        customerName: item.customerName,
                        qualityUserName: item.qualityUserName,
                        qualityTime: item.qualityTime,
                        putUserName: item.putUserName,
                        putTime: item.putTime,
                        positionNumber: item.positionNumber,
                        nodes: item.nodes,
                        detailedDate: item.detailedDate,
                    };
                })
            };
        },
        page: true,
        limit: 40,
        limits: [40, 80, 120, 200, 500],
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStoragePutDetailed.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxStoragePutDetailed.openAddDlg();
    });

    $('#reset').click(function () {
        $("#setUnit").val("")
        $("#condition").val("")
        $("#goodsName").val("")
    });

    /**
     * 点击批量打印标签按钮
     */
	HxStoragePutDetailed.btnPrint = function () {
    	var checkStatus = table.checkStatus('hxStoragePutDetailedTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
			submit = 2
	        layer.open({
	            type: 2,
	            title: '批量打印标签码',
	            area: ['900px', '550px'],
	            anim: 5,
	            content: Feng.ctxPath + '/hxStoragePutDetailed/tobtnqrCode?ids='+ids+'&printQuantity='+$("#printQuantity").val(),
	            // end: function () {
				// 	if(submit == 1){
                // 		table.reload(HxStoragePutDetailed.tableId);
				// 	}
	            // }
	        });
		} else {
			layer.alert('请至少选中一条数据');
		}
    };
    
    $('#btnPrint').click(function () {
    	HxStoragePutDetailed.btnPrint();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxStoragePutDetailed.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + HxStoragePutDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxStoragePutDetailed.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxStoragePutDetailed.onDeleteItem(data);
        }
    });
});
